#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kprobes.h>
#include <linux/futex.h>

// 定义 kprobe
static struct kprobe futex_kprobe = {
    .symbol_name = "sys_futex",  //找不到对应的符号
};

// futex 系统调用的 kprobe 处理函数
static int futex_entry_handler(struct kprobe *p, struct pt_regs *regs)
{
    printk(KERN_INFO "futex called\n");
    return 0;
}

// 模块初始化函数
static int __init futex_kprobe_init(void)
{
    int ret;

    // 注册 kprobe
    futex_kprobe.pre_handler = futex_entry_handler;
    ret = register_kprobe(&futex_kprobe);
    if (ret < 0) {
        printk(KERN_INFO "Failed to register kprobe: %d\n", ret);
        return ret;
    }

    printk(KERN_INFO "futex kprobe registered\n");
    return 0;
}

// 模块退出函数
static void __exit futex_kprobe_exit(void)
{
    unregister_kprobe(&futex_kprobe);
    printk(KERN_INFO "futex kprobe unregistered\n");
}

module_init(futex_kprobe_init);
module_exit(futex_kprobe_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple Linux kernel module to track futex calls");
MODULE_VERSION("0.1");


